package com.soft.leo.oldbook;

import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;

public class SplashActivity extends AppCompatActivity {
    private TextView timer;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        handler=new Handler();
        final Runnable runnable=new Runnable() {
            @Override
            public void run() {
                Intent intent;
                if(AVUser.getCurrentUser()!=null) {
                    intent= new Intent(SplashActivity.this, MainActivity.class);
                }
                else{
                    intent=new Intent(SplashActivity.this,LoginActivity.class);
                }
                startActivity(intent);
                finish();
            }
        };
        timer=(TextView)findViewById(R.id.splash_timer);
        MyTimer myTimer=new MyTimer(4000,1000);
        myTimer.start();
        timer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent;
                if(AVUser.getCurrentUser()!=null) {
                     intent= new Intent(SplashActivity.this, MainActivity.class);
                }
                else{
                    intent=new Intent(SplashActivity.this,LoginActivity.class);
                }

                startActivity(intent);
                handler.removeCallbacks(runnable);
                finish();
            }
        });

        handler.postDelayed(runnable,4000);
    }

    class MyTimer extends CountDownTimer{

        public MyTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            timer.setText("点击跳转("+l/1000+")");
        }

        @Override
        public void onFinish() {
            timer.setText("正在跳转");
        }
    }
}
